// 示例：如何获取RecyclerView中特定item的TextView控件

class YourActivity : AppCompatActivity() {
    private lateinit var recyclerView: RecyclerView
    private lateinit var adapter: YourAdapter
    
    // 方法1：通过ViewHolder获取TextView（推荐）
    fun getTextViewFromPosition(position: Int): TextView? {
        val holder = recyclerView.findViewHolderForAdapterPosition(position)
        return if (holder is YourAdapter.YourViewHolder) {
            holder.textView // 假设你的ViewHolder中有一个名为textView的TextView
        } else {
            null
        }
    }
    
    // 方法2：通过ViewHolder获取TextView（带安全检查）
    fun getTextViewSafely(position: Int): TextView? {
        return try {
            val holder = recyclerView.findViewHolderForAdapterPosition(position)
            if (holder is YourAdapter.YourViewHolder && holder.itemView.parent != null) {
                holder.textView
            } else {
                null
            }
        } catch (e: Exception) {
            null
        }
    }
    
    // 方法3：获取当前可见的item的TextView
    fun getCurrentVisibleTextView(): TextView? {
        val layoutManager = recyclerView.layoutManager as? LinearLayoutManager
        val position = layoutManager?.findFirstCompletelyVisibleItemPosition() ?: -1
        
        if (position != RecyclerView.NO_POSITION) {
            return getTextViewFromPosition(position)
        }
        return null
    }
    
    // 方法4：通过itemView的findViewById获取
    fun getTextViewByFindViewById(position: Int): TextView? {
        val holder = recyclerView.findViewHolderForAdapterPosition(position)
        return holder?.itemView?.findViewById(R.id.your_text_view_id)
    }
    
    // 使用示例
    fun exampleUsage() {
        // 获取第5个item的TextView
        val textView = getTextViewFromPosition(5)
        textView?.let {
            it.text = "新的文本内容"
            it.setTextColor(Color.RED)
        }
        
        // 获取当前可见item的TextView
        val currentTextView = getCurrentVisibleTextView()
        currentTextView?.let {
            it.text = "当前可见的item"
        }
    }
}

// 你的Adapter示例
class YourAdapter : RecyclerView.Adapter<YourAdapter.YourViewHolder>() {
    
    class YourViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val textView: TextView = itemView.findViewById(R.id.your_text_view_id)
        // 其他控件...
    }
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): YourViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.your_item_layout, parent, false)
        return YourViewHolder(view)
    }
    
    override fun onBindViewHolder(holder: YourViewHolder, position: Int) {
        // 绑定数据
        holder.textView.text = "Item $position"
    }
    
    override fun getItemCount(): Int = yourDataList.size
} 